package com.rf.richfitwheel.workflow.service.impl;

import com.rf.richfitwheel.admin.feignclient.FSysUserService;
import com.rf.richfitwheel.common.contants.Constant;
import com.rf.richfitwheel.workflow.conf.IamPropertiesConfig;
import com.rf.richfitwheel.workflow.feignclient.ConfigFeignService;
import com.rf.richfitwheel.workflow.model.TaskPortalTodoSync;
import com.rf.richfitwheel.workflow.service.PortalTodoSendHisService;
import com.rf.richfitwheel.workflow.service.PortalTodoSync;
import net.sf.json.JSONObject;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author hushouquan
 */
@Service
public class PortalTodoSyncImpl implements PortalTodoSync{
    @Autowired
    private RestTemplate restTemplateOuter;
    @Autowired
    private IamPropertiesConfig iamPropertiesConfig;
    @Autowired
    private TaskService taskService;
    @Autowired
    private FSysUserService fSysUserService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private PortalTodoSendHisService portalTodoSendHisService;
    @Autowired
    private ConfigFeignService configFeignService;
    /**
     * 同步待办任务
     *
     * @param processInstance   流程实例
     * @param moduleType 模块，必须要传，用来决定appId的
     * @param startUserId 发起人id
     */
    @Override
    public void todoSync(ProcessInstance processInstance, String moduleType, String startUserId, List<TaskPortalTodoSync> taskListNew){
        if(needSend(moduleType)){
            PortalTodoSyncThread portalTodoSyncThread = new PortalTodoSyncThread(iamPropertiesConfig, taskService, restTemplateOuter,
                    fSysUserService, repositoryService, historyService, runtimeService, "sync", processInstance,
                    null, null, moduleType, startUserId, portalTodoSendHisService, taskListNew);
            new Thread(portalTodoSyncThread).start();
        }
    }
    
    /**
     * 修改待办任务
     *
     * @param processInstance 流程实例
     * @param moduleType      模块，必须要传，用来决定appId的
     * @param startUserId     发起人id
     */
    @Override
    public void runSyncUpdate(ProcessInstance processInstance, String moduleType, String startUserId, List<TaskPortalTodoSync> taskListNew){
        if(needSend(moduleType)){
            PortalTodoSyncThread portalTodoSyncThread = new PortalTodoSyncThread(iamPropertiesConfig, taskService, restTemplateOuter,
                    fSysUserService, repositoryService, historyService, runtimeService, "update", processInstance,
                    null, null, moduleType, startUserId, portalTodoSendHisService, taskListNew);
            new Thread(portalTodoSyncThread).start();
        }
    }
    
    /**
     * 第一个节点处理
     *
     * @param processInstance 流程实例
     * @param taskFirst         任务
     * @param moduleType 模块，必须要传，用来决定appId的
     * @param startUserId 发起人id
     */
    @Override
    public void todoSyncFirst(ProcessInstance processInstance, Task taskFirst, String moduleType, String startUserId){
        if(needSend(moduleType)){
            PortalTodoSyncThread portalTodoSyncThread = new PortalTodoSyncThread(iamPropertiesConfig, taskService, restTemplateOuter,
                    fSysUserService, repositoryService, historyService, runtimeService, "first", processInstance, taskFirst,
                    null, moduleType, startUserId, portalTodoSendHisService, null);
            new Thread(portalTodoSyncThread).start();
        }
    }
    
    /**
     * 删除待办任务
     *
     * @param taskOld     任务
     * @param receiveUsers 要删除的任务办理人
     * @param moduleType 模块，必须要传，用来决定appId的
     */
    @Override
    public void todoSyncDelete(Task taskOld, List<String> receiveUsers, String moduleType){
        if(needSend(moduleType)){
            PortalTodoSyncThread portalTodoSyncThread = new PortalTodoSyncThread(iamPropertiesConfig, taskService, restTemplateOuter,
                    fSysUserService, repositoryService, historyService, runtimeService, "delete", null,
                    taskOld, receiveUsers, moduleType, null, portalTodoSendHisService, null);
            new Thread(portalTodoSyncThread).start();
        }
    }
    
    /**
     * 待办待阅数据置为已办或已阅
     *
     * @param taskOld     任务
     * @param receiveUsers 需要置为已办的人员
     * @param moduleType 模块，必须要传，用来决定appId的
     */
    @Override
    public void todoSyncDone(Task taskOld, List<String> receiveUsers, String moduleType){
        if(needSend(moduleType)){
            PortalTodoSyncThread portalTodoSyncThread = new PortalTodoSyncThread(iamPropertiesConfig, taskService, restTemplateOuter,
                    fSysUserService, repositoryService, historyService, runtimeService, "done", null,
                    taskOld, receiveUsers, moduleType, null, portalTodoSendHisService, null);
            new Thread(portalTodoSyncThread).start();
        }
    }
    
    /**
     * 统一任务编号下的先删除待办在新增待办，签出、签入的情况
     *
     * @param processInstance 流程实例
     * @param taskOld         流程任务
     * @param receiveUsers 要删除的接收人
     * @param moduleType 模块，必须要传，用来决定appId的
     * @param startUserId 发起人
     * @param taskListNew 新的任务
     */
    @Override
    public void runDeleteAndNew(ProcessInstance processInstance, Task taskOld, List<String> receiveUsers, String moduleType, String startUserId, List<TaskPortalTodoSync> taskListNew){
        if(needSend(moduleType)){
            PortalTodoSyncThread portalTodoSyncThread = new PortalTodoSyncThread(iamPropertiesConfig, taskService, restTemplateOuter,
                    fSysUserService, repositoryService, historyService, runtimeService, "deleteAndNew", processInstance,
                    taskOld, receiveUsers, moduleType, startUserId, portalTodoSendHisService, taskListNew);
            new Thread(portalTodoSyncThread).start();
        }
    }
    
    /**
     * 根据参数配置判断模块是否需要给门户推待办
     * @param moduleType 模块字典
     * @return 是否需要推送 true-是，false-否
     */
    private boolean needSend(String moduleType){
        try{
            String paramValue = configFeignService.getParamVlueByKey("MODULE_TYPE_TASK_SEND_PORTAL");
            JSONObject object = JSONObject.fromObject(paramValue);
            if(object.containsKey(moduleType) && Constant.COMMON_YES_OR_NO_NO.equals(object.getString(moduleType))){
                //如果参数表中给该模块配置了是否发待办且为“0”时就不发待办给门户
                return false;
            }
            return true;
        }catch(Exception e){
            return true;
        }
    }
}
